package website.seamonster.ethegg.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import website.seamonster.ethegg.dataobject.EthUser;
import website.seamonster.ethegg.enums.ResultEnum;
import website.seamonster.ethegg.exception.EthException;
import website.seamonster.ethegg.service.EthUserService;
import website.seamonster.ethegg.service.MsgService;
import website.seamonster.ethegg.service.UserService;
import website.seamonster.ethegg.service.background.TransactionService;
import website.seamonster.ethegg.utils.ResultVOUtil;
import website.seamonster.ethegg.viewobject.ResultVO;

/**
 * Created by 罗海量
 * 2018-07-24 22:30
 */
@lombok.extern.slf4j.Slf4j
@RestController
@RequestMapping("/user")
public class UserCenterController {
    @Autowired
    private UserService userService;

    @Autowired
    private EthUserService ethUserService;

    @Autowired
    private TransactionService txService;

    @Autowired
    private MsgService msgService;

    @Value("${contract.address}")
    private String contractAddress;

    //钱袋【卖蛋收益、推广收益、购买支出】
    @GetMapping("/bag")
    public ResultVO getBag(@RequestParam("userId") String userId){
        return ResultVOUtil.success(msgService.getIncomeInfo(userId));
    }

    //消息列表
    @GetMapping("/msg")
    public ResultVO getMsgList(@RequestParam("userId") String userId,
                               @RequestParam(name = "pageCount", defaultValue = "0") Integer pageCount){

        return ResultVOUtil.success(msgService.getMsgList(userId, pageCount));
    }

    //获得推广码
    @GetMapping("/pr")
    public ResultVO getPrCode(@RequestParam("accountAddress") String address){
//        return ResultVOUtil.success(userService.getPrCode(userId));
        try {
            String prCode = ethUserService.getPrCodeByAccountAddress(address);
            return ResultVOUtil.success(prCode);
        } catch (EthException e) {
            return ResultVOUtil.error(e.getCode(), e.getMessage());
        }
    }

    //根据推广码寻找对应用户Account
    @GetMapping("/prAccount")
    public ResultVO getAccountByPrCode(@RequestParam("prCode") String prCode){
        String accountAddress = ethUserService.getAccountAddressByPrCode(prCode);
        return ResultVOUtil.success(accountAddress);
    }

    @GetMapping("/contractAddress")
    public ResultVO getContractAddress(){
        return ResultVOUtil.success(contractAddress);
    }

}
